/* 使用说明：

	//初始化引擎
	var engine = new FlashCore();
	engine.setCanvas( document.getElementById("cav"));
	engine.setMouseEnabled(true); //是否支持鼠标事件
	
	//对stage进行一些参数设置
	var stage = FlashCore.stage;
	stage.setFrameRate(60) ;
	engine.start();  //引擎开始运行
	
	//下面接着向stage中添加显示对象
	........................................................
	
	
	
注：其中 DisplayObjectContainer , Stage , InteractiveObject都是不能直接实例化使用 。Sprite为容器类 ， Bitmap支持鼠标事件。
		此框架中的所有的显示对象类的dispose方法，最好不要直接去调用，
		最好在容器removeChild或者removeChildAt , removeAllChildren参数中设置dispose参数为true，框架会自动调用其dispose方法。
		并且你可以不用在显示对象中移除侦听，只要在移除时dispose为true，也会自动移除此对象以及子对象的所有侦听。
		
		默认情况下，框架不支持鼠标和键盘事件 ，需要先启用，flashCore.setKeyboardEnabled(true) 和 flashCore.setMouseEnabled(true)
		只有stage才支持键盘事件 。
*/

var FLASH = window.FLASH || {};
/**
 * 继承
 * @param subclass 本类
 * @param superclass 父类
 */
FLASH.extend = function(subclass, superclass) 
{
    var f = function() {};
     f.prototype = superclass.prototype;
     subclass.prototype = new f();
     subclass.prototype.constructor = subclass;
     subclass.superclass = superclass.prototype;
    if (superclass.prototype.constructor == Object.prototype.constructor) {
         superclass.prototype.constructor = superclass;
     }
};
//=====================================================
//======================================================
/**
 * 程序入口
 */
FlashCore = function(){
	if(FlashCore._instance!=undefined || FlashCore._instance!=null) return ;
	
	if(!FlashCore.stage){
		FlashCore.stage = new Stage();
		FlashCore.stage._stage = FlashCore.stage ;
	}
	this._ctrlKey = false ;
	this._altKey = false ;
	this._shiftKey = false ;
};

/**
 * 获取stage对象
 */
FlashCore.stage ;
/**
 * 主画布
 */
FlashCore.canvas;

/**
 * 获取当前对象的实例，单例
 * @returns
 */
FlashCore.getInstance  = function()
{
	if(FlashCore._instance==undefined || FlashCore._instance==null){
		FlashCore._instance = new FlashCore();
	}
	return FlashCore._instance ;
};

/**
 * 设置画布
 * @canvas 画布
 * @return 此实例
 */
FlashCore.prototype.setCanvas = function( canvas )
{
	FlashCore.canvas = canvas ;
 	canvas.focus();  // 获得焦点之后，才能够对键盘事件进行捕
	FlashCore.stage._context = FlashCore.canvas.getContext("2d");
	FlashCore.stage._stageWidth = canvas.width ;
	FlashCore.stage._stageHeight = canvas.height ;
	return this; 
};

FlashCore.prototype.start = function()
{
	FlashCore.stage.start();
};

FlashCore.prototype.pause = function()
{
	FlashCore.stage.pause();
};

FlashCore.prototype.sleep = function()
{
	FlashCore.stage.sleep();
};

FlashCore.prototype.wakeUp = function()
{
	FlashCore.stage.wakeUp();
};

FlashCore.prototype.setMouseEnabled = function( value )
{
	if(value)
	{
		var _self = this ;
		//鼠标移动事件 
		FlashCore.canvas.onmousemove = function(e)
		{
			FlashCore.stage._onMouseMove( e.pageX-FlashCore.canvas.offsetLeft , e.pageY-FlashCore.canvas.offsetTop ,
					this._ctrlKey,  this._altKey,  this._shiftKey);
		};
		
		//鼠标按下
		FlashCore.canvas.onmousedown = function(e)
		{
			FlashCore.stage._onMouseDown(this._ctrlKey,  this._altKey,  this._shiftKey);
		};
		
		//鼠标up
		FlashCore.canvas.onmouseup = function(e)
		{
			FlashCore.stage._onMouseUp(this._ctrlKey,  this._altKey,  this._shiftKey);
		};
	}
	else
	{
		FlashCore.canvas.onmousemove = null ;
		FlashCore.canvas.onmousedown = null;
		FlashCore.canvas.onmouseup = null;
	}
};

/**
 * 是否支持键盘事件 
 * @param value true表示支持，false表示不支持
 */
FlashCore.prototype.setKeyboardEnabled = function( value )
{
	if( value)
	{
		var _self = this ;
		window.onkeydown=function(e)
		{
			_self._shiftKey = e.shiftKey ;
			_self._ctrlKey = e.ctrlKey ;
			_self._altKey = e.altKey ;
			FlashCore.stage._onKeyDown(e.charCode, e.keyCode, e.keyLocation,  _self._ctrlKey,  _self._altKey,  _self._shiftKey );
		};
		
		window.onkeyup=function(e)
		{
			FlashCore.stage._onKeyUp(e.charCode, e.keyCode, e.keyLocation,  _self._ctrlKey,  _self._altKey,  _self._shiftKey );
		};
	}
	else
	{
		window.onkeydown = null ;
		window.onkeyup = null ;
	}
};

/**
 * 禁用网页中鼠标右键菜单
 */
FlashCore.prototype.hideRightMenu = function()
{
	FlashCore.canvas.addEventListener("contextmenu", function (e) { e.preventDefault(); }, false);
	if (window.navigator.msPointerEnabled) {
		document.addEventListener("MSGestureHold", function (e) { e.preventDefault(); }, false);
	}	
};